package com.li.bbs;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class TestUseTree {
	public static void main(String[] args) {
		
//		ArrayList<Article> list = new ArrayList<Article>();
//		
//		
//		try {	
//			Connection conn = DB.getConn();
//			getTree(list,conn, 0, 0);
//			for(int i=0; i<list.size(); i++){
//				System.out.println(list.get(i));
//			}
//			
//			DB.free(conn);
//			 
//		
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
		
		
	}

	public static void getTree(ArrayList<Article> list, Connection conn, int id, int grade) throws SQLException {
		
		
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery("select * from article where pid=" + id );
		
		while(rs.next()){
			Article article = new Article();
			article.setId(rs.getInt("id"));
			article.setPid(rs.getInt("pid"));
			article.setRootid(rs.getInt("rootid"));
			article.setTitle(rs.getString("title"));
			article.setPdate(rs.getDate("pdate"));
			article.setGrade(grade);
			
			list.add(article);
			getTree(list, conn, article.getId(),grade+1);
			
		}
		DB.free(rs, stmt);
		
	}
	
	public static void getTopArticle(ArrayList<Article> list, Connection conn, int grade) throws SQLException {
		
		
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery("select * from article where pid=0" );
		
		while(rs.next()){
			Article article = new Article();
			article.setId(rs.getInt("id"));
			article.setPid(rs.getInt("pid"));
			article.setRootid(rs.getInt("rootid"));
			article.setTitle(rs.getString("title"));
			article.setPdate(rs.getDate("pdate"));
			article.setGrade(grade);
			
			list.add(article);
			
			
		}
		DB.free(rs, stmt);
		
	}

}
